package org.example.curriculum_design_6_16.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.curriculum_design_6_16.entity.pojo.SocialServiceRule;
import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.service.SocialServiceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/socialServiceRule")
public class SocialServiceRuleController {

    @Autowired
    private SocialServiceRuleService socialServiceRuleService;

    /**
     * 获取所有活跃的评分规则
     * @return JSON格式的规则列表
     */
    @GetMapping
    public Result getAllRules() {
        return Result.success(socialServiceRuleService.getAllActiveRules());
    }

    /**
     * 按类型获取评分规则
     * @param ruleType 规则类型(position/activity/other)
     * @return JSON格式的规则列表
     */
    @GetMapping("/type/{ruleType}")
    public Result getRulesByType(@PathVariable String ruleType) {
        return Result.success(socialServiceRuleService.getRulesByType(ruleType));
    }

    /**
     * 获取职位类评分规则映射表
     * @return JSON格式的职位规则映射表
     */
    @GetMapping("/position-map")
    public Result getPositionRuleMap() {
        return Result.success(socialServiceRuleService.getPositionRuleMap());
    }

    /**
     * 添加评分规则
     * @param rule 评分规则JSON对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result addRule(@RequestBody SocialServiceRule rule) {
        try {
            int result = socialServiceRuleService.addRule(rule);
            return result > 0 ? Result.success(result) : Result.error(500, "添加评分规则失败");
        } catch (Exception e) {
            log.error("添加评分规则失败", e);
            return Result.error(500, "添加评分规则失败: " + e.getMessage());
        }
    }

    /**
     * 更新评分规则
     * @param rule 评分规则JSON对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<Boolean> updateRule(@RequestBody SocialServiceRule rule) {
        try {
            int result = socialServiceRuleService.updateRule(rule);
            return result > 0 ? Result.success(true) : Result.error(500, "更新评分规则失败");
        } catch (Exception e) {
            log.error("更新评分规则失败", e);
            return Result.error(500, "更新评分规则失败: " + e.getMessage());
        }
    }

    /**
     * 启用/禁用评分规则
     * @param request 包含状态变更请求的JSON对象
     * @return 操作结果
     */
    @PatchMapping("/{id}/status")
    public Result<Boolean> toggleRuleStatus(
            @PathVariable Integer id,
            @RequestBody Map<String, Boolean> request) {
        try {
            Boolean active = request.get("active");
            if (active == null) {
                return Result.error(400, "参数active不能为空");
            }
            int result = socialServiceRuleService.toggleRuleStatus(id, active);
            return result > 0 ? Result.success(true) : Result.error(500, "更新规则状态失败");
        } catch (Exception e) {
            log.error("更新规则状态失败", e);
            return Result.error(500, "更新规则状态失败: " + e.getMessage());
        }
    }
}